<?php

namespace app\admin\controller\financial;

use service\JsonService;
use app\common\model as M;

/**
 * 充值模块
 */
class Recharge extends \app\admin\controller\financial\Order
{
	public $model = '\app\common\model\order\UserRecharge';

    public function save ()
    {
        $post = $this->request->post();

        if ( !isset($post['user_info']) || !is_array($post['user_info']) ) return JsonService::fail('缺少用户信息');
        if ( M\User::where(['uid' => $post['user_info']['uid'],'status' => 1])->count() == 0 ) return JsonService::fail('用户无效');

        $user = M\User::where('uid' , $post['user_info']['uid'])->find();

        if ( empty($user) ) return JsonService::fail('用户不存在');
        if ( $user->status != 1 ) return JsonService::fail('用户无效');

        $this->model::beginTrans();

        $recharge = (new $this->model);
        try {
            $org_id    = $this->model::getBelongOrganId();
            $school_id = $this->model::getBelongSchoolId();

            ## 创建订单
            $order['orgn_amt']       = $post['now_money'] ?? 0;
            $order['disc_amt']       = 0;
            $order['order_amt']      = $order['orgn_amt'] - $order['disc_amt'];
            $order['uid']            = $post['user_info']['uid'];
            $order['pub_aid']        = $this->adminId;
            $order['remark']         = $post['remark'] ?? '';
            $order['org_id']         = ( $org_id > 0 ) ? $org_id : $user->org_id;
            $order['school_id']      = ( $school_id > 0 ) ? $school_id : $user->school_id;
            $order['platform']       = M\Order::PTF_BACKEND;
            $order_id = \app\common\model\order\UserRecharge::create_order($order);
            if ( !$order_id ) {
                return JsonService::fail('创建订单失败【0981】');
            }
            $order['order_id']        = $order_id;

            $recharge->uid            = $order['uid'];
            $recharge->level_id       = $post['user_info']['level_id'];
            $recharge->remark         = $post['remark'] ?? '';
            $recharge->order_id       = $order_id;
            $recharge->create_time    = date('Y-m-d H:i:s');
            $recharge->order_date     = $post['order_date'] ?? date('Y-m-d');
            $recharge->now_money      = $post['now_money'] ?? 0;
            $recharge->gift_money     = $post['gift_money'] ?? 0;
            $recharge->org_id         = $order['org_id'];
            $recharge->school_id      = $order['school_id'];

            $res = $recharge->save();
            if ( !$res ) return JsonService::fail('充值失败【0982】');

            $this->model::commitTrans();
            return JsonService::successful('创建订单成功，请完成支付' , $order);
        } catch ( \Exception $e ) {
            $this->model::rollbackTrans();
            return JsonService::fail($e->getMessage());
        }

        $this->model::rollbackTrans();
        return JsonService::fail('操作失败');
    }

    /**
     * [_confirm_check description]
     * @param    array            $post [description]
     * @return   [type]                 [description]
     */
    public function _confirm_check ( array $post )
    {
        if ( isset($post['pay_type']) && $post['pay_type'] == M\Order::PAY_BALANCE ) {
            return '充值订单无法使用余额支付';
        }
        return '';
    }
} //Class End